var app = new Vue({
  el: "#shareAndCorrection",
  data() {
    return {
      showType: 0, // 0 价格分享/纠错商品列表，1 价格分享/纠错历史, 2 价格分享/纠错
      oldShowType: 0,
      headerTitle: "价格纠错/分享",
      headerRight_btn: "历史",
      changeMarketTitle: "", // 选择超市或修改超市,
      // 超市名和超市ID
      market: {
        marketName: "",
        supermarketId: null
      },
      selectedLabel: "审核", // 商品列表切换tab当前选择
      tabLabels: [
        {
          label: "常规"
        },
        {
          label: "前100名"
        },
        {
          label: "价格过期"
        }
      ],
      loop: false,
      autoPlay: false,
      showDots: false,
      slideOptions: {
        listenScroll: true,
        probeType: 3,
        directionLockThreshold: 0
      },
      currentPage: 0,
      pageSize: 6,
      totalPage: 2,
      pullUpLoad: false,
      pullUpLoadThreshold: 0,
      pullUpLoadNoMoreTxt: "没有更多了！",
      routineData: [], // 常规商品列表数据
      top100Data: null, // 前100名商品列表数据
      overdueData: null, // 价格过期商品列表数据
      marketKey: "", // 超市名搜索关键字
      marketData: null, // 超市搜索结果列表数据
      fullName: "", // 筛选商品名
      brandName: "", // 筛选品牌名
      valueName: "", // 筛选规格
      barcode: "", // 筛选条码
      categoryName: "",
      categoryId: null,
      categoryIdArray: [],
      categoryData: [],
      brandData: null, // 筛选品牌下拉列表数据
      isInput: false, // 品牌输入框是否获取焦点
      historyData: [], // 历史列表数据
      createTimeStart: "", // 历史按日期筛选开始时间
      createTimeEnd: "", // 历史按日期筛选结束时间
      params: {
        // 商品分享纠错所传参数
        supermarketId: null, //超市id
        goodsSkuId: null, //商品id
        type: "请选择价格类型", //用户选择的类型[价格类型]正常价：NORMAL促销价：PROMOTION处理价：DEAL 会员价：MEMBER
        startTime: "请选择开始时间",
        endTime: "请选择结束时间",
        price: null, //价格
        priceType: "", //价格类型  没价格：NO_PRICE待审核：WAIT_AUDIT已生效：AFFECTED
        taskId: null //任务id
      },
      showPrice: null, // 商品分享纠错价格
      imageSrc: "", // 商品分享纠错商品图片
      btnDisabled: false, // 商品分享纠错提交按钮是否禁用
      btnTitle: "", // 商品分享纠错提交按钮显示文字
      goodsPrice: "",
      priceData: null, // 历史记录查看价格弹窗数据
      tabTitle: [
        {
          title: "抢单"
        },
        {
          title: "待完成"
        }
      ],
      tabTitleIndex: 0,
      listData: [],
      checkModel: [],
      checkedAll: false,
      errMsg: "",
      loading: false,
      popup: false,
      pop: {
        show: false,
        grabSheetNot: null, //被别人抢走的数量
        grabSheetPass: null //抢到的数量
      },
      skuInfo: null,
      screenType: null //筛选 SHARE 分享 CORRECT 纠错 correct/correct/getTaskListBySupermarketId.do correct/correct/getTaskListBySupermarketId.do
    };
  },
  watch: {
    marketKey(val) {
      if (val.length > 0) {
        this.pullUpLoad = true;
        this.currentPage = 1;
        this.getMarketData();
      } else {
        this.marketData = null;
        this.pullUpLoad = false;
      }
    },
    brandName(val) {
      if (val.length > 0 && this.isInput) {
        this.getBrandData(val);
      }
    },
    params(val) {
      console.log(val);
    },
    checkModel: {
      handler() {
        if (
          this.checkModel.length === this.listData.length &&
          this.listData != 0
        ) {
          this.checkedAll = true;
        } else {
          this.checkedAll = false;
        }
        this.procurementTaskIdList = this.checkModel.join(",");
      },
      deep: true // 深度监视
    }
  },

  computed: {
    initialIndex() {
      let index = 0;
      index = this.findIndex(
        this.tabLabels,
        item => item.label === this.selectedLabel
      );
      return index;
    },
    scrollOptions() {
      return {
        pullUpLoad: this.pullUpLoadObj
      };
    },
    pullUpLoadObj: function () {
      return this.pullUpLoad
        ? {
          threshold: parseInt(this.pullUpLoadThreshold),
          txt: {
            noMore: this.pullUpLoadNoMoreTxt
          }
        }
        : false;
    }
  },
  created() {
    let market = JSON.parse(localStorage.getItem("market"));
    if (market) {
      this.market.marketName = market.marketName;
      this.market.supermarketId = market.supermarketId;
      this.params.supermarketId = market.supermarketId;
      this.changeMarketTitle = "选择超市";
    } else {
    }
    let params = getUrlParams();
    if (Object.keys(params).length > 0) {
      this.brandName = decodeURI(params.brandName);
      this.fullName = decodeURI(params.goodsName);
      this.valueName = decodeURI(params.attrValue);
    }
  },
  mounted() {
    let params = getUrlParams();
    if (Object.keys(params).length > 0) {
      this.screenPopup();
    }
  },
  methods: {
    // 返回
    goback() {
      if (this.showType === 0) {
        window.history.go(-1);
      }
      if (this.showType === 2) {
        this.resetShareAndCorrection();
        this.showType = 0;
        this.tabTitleIndex = 1;
      } else if (this.showType === 1) {
        this.currentPage = 1;
        this.pullUpLoad = false;
        this.routineData = null;
        this.createTimeStart = "";
        this.createTimeEnd = "";
        this.selectedLabel = "常规";
        this.showType = 0;
        this.headerTitle = "价格纠错/分享";
        this.headerRight_btn = "历史"
      } else {
        if (this.oldShowType === 0) {
          this.currentPage = 1;
          this.pullUpLoad = false;
          this.routineData = null;
          this.selectedLabel = "常规";
          this.getRoutineData();
          this.showType = 0;
          this.headerTitle = "价格纠错/分享";
        } else if (this.oldShowType === 1) {
          /* this.currentPage = 1
                    this.pullUpLoad = false
                    this.historyData = null
                    this.getHistoryData() */
          this.createTimeStart = "";
          this.createTimeEnd = "";
          this.showType = 1;
          this.headerTitle = "历史";
        }
      }
    },

    // 更改tab
    changeType(index) {
      let self = this;
      self.tabTitleIndex = index;
      self.loading = true;
      self.currentPage = 1;
      self.listData = [];
      self.getData();
    },
    // 切换到分享/纠错历史
    showHistory() {
      if (this.headerRight_btn === "筛选") {
        this.historyScreen();
      } else {
        this.currentPage = 1;
        this.pullUpLoad = false;
        this.getHistoryData();
        this.showType = 1;
        this.headerTitle = "历史";
        this.headerRight_btn = "筛选";
      }
    },
    // 分享/纠错历史筛选按钮
    dateSegmentData() {
      let dateSegmentData = [
        {
          is: "cube-date-picker",
          title: "开始时间",
          min: new Date(2019, 0, 1),
          max: new Date(2030, 11, 31)
        },
        {
          is: "cube-date-picker",
          title: "结束时间",
          min: new Date(2019, 0, 1),
          max: new Date(2030, 11, 31)
        }
      ];
      this.dateSegmentPicker = this.$createSegmentPicker({
        data: dateSegmentData,
        onSelect: (selectedDates, selectedVals, selectedTexts) => {
          this.createTimeStart = selectedTexts[0].join("-") + " 00:00:00";
          this.createTimeEnd = selectedTexts[1].join("-") + " 23:59:59";
          this.currentPage = 1;
          this.pullUpLoad = false;
          this.historyData = null;
          this.getHistoryData();
          console.log();
        },
        onNext: (i, selectedDate, selectedValue, selectedText) => {
          dateSegmentData[1].min = selectedDate;
          if (i === 0) {
            this.dateSegmentPicker.$updateProps({
              data: dateSegmentData
            });
          }
        }
      });
    },
    showDateSegmentPicker() {
      this.dateSegmentData();
      this.dateSegmentPicker.show();
    },
    findIndex(ary, fn) {
      if (ary.findIndex) {
        return ary.findIndex(fn);
      }
      let index = -1;
      ary.some(function (item, i, ary) {
        const ret = fn.call(this, item, i, ary);
        if (ret) {
          index = i;
          return ret;
        }
      });
      return index;
    },
    // 分享纠错商品列表tab切换
    clickHandler(label) {
      this.selectedLabel = label;
    },
    // 分享纠错商品列表滑动切换
    changePage(index) {
      this.currentPage = 1;
      this.pullUpLoad = false;
      if (index === 0) {
        this.$refs.routine.scrollTo(0, 0, 200, "linear");
        this.getRoutineData();
      } else if (index === 1) {
        this.$refs.top100.scrollTo(0, 0, 200, "linear");
        this.getTop100Data();
      } else if (index === 2) {
        this.$refs.overdue.scrollTo(0, 0, 200, "linear");
        this.getOverdueData();
      }
      this.selectedLabel = this.tabLabels[index].label;
    },
    // 选择超市弹窗展开
    changeMarket() {
      this.marketKey = "";
      this.marketData = null;
      const choiceMarket = this.$refs.choiceMarket;
      choiceMarket.show();
    },
    // 筛选弹窗展开
    screenPopup() {
      this.categoryData = [];
      this.categoryIdArray = [];
      Axios.post("goods/category/showAllCategory.do").then(res => {
        if (res.respCode === "S") {
          let list = res.data;
          list.forEach(item => {
            this.categoryData.push(item);
            this.categoryIdArray.push(item.categoryId);
          });
        }
      });
      const screenPopup = this.$refs.screenPopup;
      screenPopup.show();
    },
    changeSelect(value, index, text) {
      this.categoryName = value;
      this.categoryId = this.categoryIdArray[index];
    },
    // 无限加载常规商品列表数据
    routineDataMore() {
      this.currentPage++;
      if (this.currentPage > this.totalPage) {
        this.$refs.routine.forceUpdate();
        return;
      }
      this.getRoutineData();
    },
    // 获取常规商品列表数据
    getRoutineData() {
      Axios.post("goods/goods/vagueGoodsSku.do", {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        supermarketId: this.market.supermarketId,
        fullName: this.fullName,
        brandName: this.brandName,
        valueName: this.valueName,
        barcode: this.barcode,
        categoryId: this.categoryId
      }).then(res => {
        if (res.respCode === "S") {
          let list = res.data.data;
          if (this.currentPage === 1) {
            this.routineData = list;
          } else {
            this.routineData = this.routineData.concat(list);
          }
          this.totalPage = res.data.totalPage;

          if (this.currentPage >= this.totalPage) {
            this.$refs.routine.forceUpdate();
          } else {
            this.pullUpLoad = true;
          }
        }
      });
    },
    // 无限加载前100名商品列表数据
    top100DataMore() {
      this.currentPage++;
      if (this.currentPage > this.totalPage) {
        this.$refs.top100.forceUpdate();
        return;
      }
      this.getTop100Data();
    },
    // 获取前100名商品列表数据
    getTop100Data() {
      Axios.post("goods/goods/highclicknopricegoods.do", {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        supermarketId: this.market.supermarketId,
        fullName: this.fullName,
        brandName: this.brandName,
        valueName: this.valueName,
        barcode: this.barcode,
        categoryId: this.categoryId
      }).then(res => {
        if (res.respCode === "S") {
          let list = res.data.data;
          if (this.currentPage === 1) {
            this.top100Data = list;
          } else {
            this.top100Data = this.top100Data.concat(list);
          }
          this.totalPage = res.data.totalPage;

          if (this.currentPage >= this.totalPage) {
            this.$refs.top100.forceUpdate();
          } else {
            this.pullUpLoad = true;
          }
        }
      });
    },
    // 无限加载价格过期商品列表数据
    overdueDataMore() {
      this.currentPage++;
      if (this.currentPage > this.totalPage) {
        this.$refs.overdue.forceUpdate();
        return;
      }
      this.getOverdueData();
    },
    // 获取价格过期商品列表数据
    getOverdueData() {
      Axios.post("goods/goods/getGoodsSkuNotPrice.do", {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        supermarketId: this.market.supermarketId,
        fullName: this.fullName,
        brandName: this.brandName,
        valueName: this.valueName,
        barcode: this.barcode,
        categoryId: this.categoryId
      }).then(res => {
        if (res.respCode === "S") {
          let list = res.data.data;
          if (this.currentPage === 1) {
            this.overdueData = list;
          } else {
            this.overdueData = this.overdueData.concat(list);
          }
          this.totalPage = res.data.totalPage;

          if (this.currentPage >= this.totalPage) {
            this.$refs.overdue.forceUpdate();
          } else {
            this.pullUpLoad = true;
          }
        }
      });
    },
    // 右侧选择超市弹窗无限加载超市列表数据
    marketDataMore() {
      this.currentPage++;
      if (this.currentPage > this.totalPage) {
        this.$refs.marketData.forceUpdate();
        return;
      }
      this.getMarketData();
    },
    // 右侧选择超市弹窗获取超市列表数据
    getMarketData() {
      Axios.post("area/Supermarket/getSupermarketByKey.do", {
        key: this.marketKey,
        currentPage: 1,
        pageSize: 20
      }).then(res => {
        if (res.respCode === "S") {
          let list = res.data.data;
          let reg = new RegExp(this.marketKey, "g");
          list.forEach(item => {
            item.name = item.name.replace(reg, "<i>" + this.marketKey + "</i>");
          });
          if (this.currentPage === 1) {
            this.marketData = list;
          } else {
            this.marketData = this.marketData.concat(list);
          }
          this.totalPage = res.data.totalPage;

          if (this.currentPage >= this.totalPage) {
            this.$refs.marketData.forceUpdate();
          }
        }
      });
    },
    // 右侧选择超市弹窗选择超市
    /**
     *
     * @param {
     * fullName: "广东省深圳市龙华区观澜街道"
     * name: "天虹商场(观澜店)"
     * supermarketId: 27
     * }
     */
    choiceMarket(item) {
      let reg = new RegExp("<i>", "g");
      let reg1 = new RegExp("</i>", "g");
      item.name = item.name.replace(reg, "");
      item.name = item.name.replace(reg1, "");
      this.market.marketName = item.name;
      this.market.supermarketId = item.supermarketId;

      this.params.supermarketId = item.supermarketId;
      localStorage.setItem("market", JSON.stringify(this.market));
      this.changeMarketTitle = "修改超市";
      const choiceMarket = this.$refs.choiceMarket;
      choiceMarket.hide();
      this.currentPage = 1;
      this.listData = null;
      this.loading = false;

      this.getData();
    },
    // 右侧筛选弹窗品牌输入框获取下拉品牌列表数据
    getBrandData(val) {
      Axios.post("brand/brand/searchBrand.do", {
        brandName: val
      }).then(res => {
        if (res.respCode === "S") {
          this.brandData = res.data.slice(0, 10);
        }
      });
    },
    // 右侧筛选弹窗品牌输入框获取焦点
    getBrandFocus() {
      this.isInput = true;
    },
    // 右侧筛选弹窗品牌输入框下拉品牌列表选择品牌
    changeBrand(item) {
      this.isInput = false;
      this.brandName = item.name;
      this.brandData = null;
    },
    // 右侧筛选弹窗重置按钮
    btnReset() {
      this.brandName = "";
      this.brandData = null;
      this.fullName = "";
      this.valueName = "";
      this.barcode = "";
      this.categoryName = "";
      this.categoryId = null;
    },
    // 右侧筛选弹窗提交按钮
    btnSubmit() {
      const screenPopup = this.$refs.screenPopup;
      screenPopup.hide();
      this.currentPage = 1;
      this.pullUpLoad = false;
      if (this.selectedLabel === "常规") {
        this.routineData = null;
        this.getRoutineData();
      } else if (this.selectedLabel === "前100名") {
        this.top100Data = null;
        this.getTop100Data();
      } else if (this.selectedLabel === "价格过期") {
        this.overdueData = null;
        this.getOverdueData();
      }
    },
    // 无限加载分享/纠错历史列表数据
    historyDataMore() {
      this.currentPage++;
      if (this.currentPage > this.totalPage) {
        this.$refs.historyData.forceUpdate();
        return;
      }
      this.getHistoryData();
    },
    /**
     * TODO
     * 获取分享/纠错历史列表数据
     * supermarketId
     * strStartTime
     * strEndTime
     */
    getHistoryData() {
      Axios.post("correct/correct/getAuditTaskHistory.do", {
        currentPage: this.currentPage,
        startTime: this.startTime,
        strEndTime: this.endTime,
        pageSize: this.pageSize,


      }).then(res => {
        if (res.respCode === "S") {
          let list = res.data.data;
          list.forEach(item => {
            item.createTime = new Date(item.dealTime).Format("yy/MM/dd");
            item.price = item.priceAudit / 100 + "元";
          });
          if (this.currentPage === 1) {
            this.historyData = list;
          } else {
            this.historyData = this.historyData.concat(list);
          }
          this.totalPage = res.data.totalPage;

          if (this.currentPage >= this.totalPage) {
            this.$refs.historyData.forceUpdate();
          } else {
            this.pullUpLoad = true;
          }
        }
      });
    },
    // 选择价格类型
    choicePriceType() {
      this.$createActionSheet({
        data: [
          {
            content: "正常价"
          },
          {
            content: "促销价"
          },
          {
            content: "处理价"
          },
          {
            content: "会员价"
          }
        ],
        onSelect: (item, index) => {
          this.params.type = item.content;
        },
        onCancel: () => { }
      }).show();
    },
    // 开始时间弹窗
    choiceStartAndEndTime() {
      this.startAndEndTime();
      this.startAndEndTimePicker.show();
    },
    startAndEndTime() {
      let dateSegmentData = [
        {
          is: "cube-date-picker",
          title: "开始时间",
          min: new Date(),
          max: new Date(2030, 12, 31, 23, 59),
          columnCount: 5
        },
        {
          is: "cube-date-picker",
          title: "结束时间",
          min: new Date(),
          max: new Date(2030, 12, 31, 23, 59),
          columnCount: 5
        }
      ];
      this.startAndEndTimePicker = this.$createSegmentPicker({
        data: dateSegmentData,
        onSelect: (selectedDates, selectedVals, selectedTexts) => {
          let startTime = selectedTexts[0];
          let endTime = selectedTexts[1];
          this.params.startTime =
            startTime[0] +
            "-" +
            startTime[1] +
            "-" +
            startTime[2] +
            " " +
            startTime[3] +
            ":" +
            startTime[4];
          this.params.endTime =
            endTime[0] +
            "-" +
            endTime[1] +
            "-" +
            endTime[2] +
            " " +
            endTime[3] +
            ":" +
            endTime[4];
        },
        onNext: (i, selectedDate, selectedValue, selectedText) => {
          dateSegmentData[1].min = selectedDate;
          if (i === 0) {
            this.startAndEndTimePicker.$updateProps({
              data: dateSegmentData
            });
          }
        }
      });
    },
    // 图片预览
    showImage() {
      let self = this;
      let inputDOM = this.$refs.uploadFile;
      // 通过DOM取文件数据
      this.params.file = inputDOM.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
        var srcString = evt.target.result;
        //安卓获取的base64数据无信息头，加之
        if (srcString.substring(5, 10) != "image") {
          srcString = srcString.replace(/(.{5})/, "$1image/jpeg;");
        }
        self.imageSrc = srcString;
      };
      if (self.params.file) {
        reader.readAsDataURL(self.params.file);
      }
    },
    // 验证数据是否合法
    /***
     *  supermarketId: null, //超市id
        goodsSkuId: null, //商品id
        price: null, //价格
        priceType: "", //价格类型  没价格：NO_PRICE待审核：WAIT_AUDIT已生效：AFFECTED
        startTime: "请选择开始时间",
        endTime: "请选择结束时间",
        correctId: "", //纠错id 有就必须传
        taskId: null, //任务id
        type: "" //用户选择的类型[价格类型]正常价：NORMAL促销价：PROMOTION处理价：DEAL 会员价：MEMBER
     */
    checkParam() {
      let data = this.params;
      for (let i in data) {
        if (i === "type") {
          if (data[i] === "请选择价格类型" || data[i] === "") {
            this.toast = this.$createToast({
              txt: "请填选择价格类型",
              type: "txt"
            });
            this.toast.show();
            return false;
          }
        } else if (i === "startTime") {
          if (data[i] === "请选择开始时间" || data[i] === "") {
            this.toast = this.$createToast({
              txt: "请选择开始时间",
              type: "txt"
            });
            this.toast.show();
            return false;
          }
        } else if (i === "endTime") {
          if (data[i] === "请选择结束时间" || data[i] === "") {
            this.toast = this.$createToast({
              txt: "请选择结束时间",
              type: "txt"
            });
            this.toast.show();
            return false;
          }
        } else if (i === "price") {
          if (data[i] === 0 || data[i] === null) {
            this.toast = this.$createToast({
              txt: "请输入商品价格",
              type: "txt"
            });
            this.toast.show();
            return false;
          }
          //纠错分享价格等于输入价格可通过(hzq)
        }
      }
      return true;
    },
    // 分享/纠错提交
    shareAndCorrectionSubmit() {
      let self = this;
      let data = this.params;
      data.goodsSkuId = self.listData[self.skuInfo.index].goodsSkuId;
      data.priceType = self.listData[self.skuInfo.index].priceType;
      data.taskId = self.listData[self.skuInfo.index].taskId;
      if (self.listData[self.skuInfo.index].correctId) {
        data.correctId = self.listData[self.skuInfo.index].correctId;
      }
      this.btnDisabled = true;
      data.price = Math.round(this.showPrice * 100);
      let param = new FormData();
      if (this.checkParam()) {
        for (let i in data) {
          if (i === "type") {
            if (data[i] === "正常价") {
              param.append(i, "NORMAL");
            } else if (data[i] === "促销价") {
              param.append(i, "PROMOTION");
            } else if (data[i] === "处理价") {
              param.append(i, "DEAL");
            } else if (data[i] === "会员价") {
              param.append(i, "MEMBER");
            }
          } else {
            param.append(i, data[i]);
          }
        }
      } else {
        this.btnDisabled = false;
        return;
      }
      console.log(param);
      Axios.post("/correct/correct/updatePrice.do", param)
        .then(res => {
          if (res.respCode === "S") {
            this.toast = this.$createToast({
              txt: res.data,
              type: "txt"
            });
            this.toast.show();
            if (this.oldShowType === 0) {
              this.currentPage = 1;
              this.createTimeStart = "";
              this.createTimeEnd = "";
              this.resetShareAndCorrection();
              this.listData = [];
              this.getData();
              this.showType = 0;
              this.headerTitle = "价格纠错/分享";
            } else if (this.oldShowType === 1) {
              this.currentPage = 1;
              this.getData();
              this.showType = 1;
              this.headerTitle = "历史";
            }
            this.btnDisabled = false;
          } else {
            this.toast = this.$createToast({
              txt: res.errorMsg,
              type: "txt"
            });
            this.toast.show();
          }
        })
        .catch(err => {
          console.error(err);
        });
    },
    // 重置分享纠错数据
    resetShareAndCorrection() {
      // 商品分享纠错所传参数
      this.params.type = "请选择价格类型";
      this.params.goodsSkuId = null;
      this.params.startTime = "请选择开始时间";
      this.params.endTime = "请选择结束时间";
      this.params.price = null;
      this.params.priceType = "";
      this.showPrice = null;
      this.correctId = null;
      this.taskId = null;
      this.imageSrc = "";
      this.supermarketId = null;
    },
    // 分享纠错按钮
    btnShareAndCorrection(item) {
      this.oldShowType = this.showType;
      this.params.supermarketId = this.market.supermarketId;
      this.params.correctType = item.correctType;
      this.params.goodsSkuId = item.goodsSkuId;
      this.goodsPrice = item.goodsPrice / 100;
      this.resetShareAndCorrection();
      this.showType = 2;
      if (item.correctType === "SHARE" || item.correctType === "VIEW") {
        this.headerTitle = "价格分享";
        this.btnTitle = "确认分享";
      } else if (item.correctType === "CORRECT") {
        this.headerTitle = "价格纠错";
        this.btnTitle = "确认纠错";
      }
      if (item.correctId) {
        this.params.correctId = item.correctId;
      }
    },

    // 撤销价格按钮
    revokePrice(index) {
      Axios.post("correct/correct/recall.do", {
        correctId: this.historyData[index].correctId
      })
        .then(res => {
          if (res.respCode == "S") {
            this.toast = this.$createToast({
              txt: "撤销成功！",
              type: "txt"
            });
            this.toast.show();
            this.historyData[index].historyButtonType = "VIEW";
          }
        })
        .catch(err => {
          console.error(err);
        });
    },
    // 查看价格按钮
    viewPrice(index) {
      Axios.post("correct/correct/historyView.do", {
        correctId: this.historyData[index].correctId
      })
        .then(res => {
          if (res.respCode == "S") {
            let data = res.data;
            data.price = data.price / 100;
            this.priceData = data;
            const viewPrice = this.$refs.viewPrice;
            viewPrice.show();
          }
        })
        .catch(err => {
          console.error(err);
        });
    },
    // 价格纠错按钮
    errorCorrection(index) {
      this.params.supermarketId = this.historyData[index].supermarketId;
      this.params.goodsSkuId = this.historyData[index].goodsSkuId;
      this.params.correctId = this.historyData[index].correctId;
      this.params.correctType = "CORRECT";
      this.oldShowType = this.showType;
      this.goodsPrice = this.historyData[index].price / 100;
      this.resetShareAndCorrection();
      this.showType = 2;
      this.headerTitle = "价格纠错";
      this.btnTitle = "确认纠错";
    },
    changeOne($even, id) {
      if ($even.target.checked) {
        this.checkModel.push(id);
      } else {
        this.checkModel.splice(this.checkModel.indexOf(id), 1);
      }
    },
    // 滚动加载
    loadMore() {
      let self = this;
      self.currentPage++;
      if (self.currentPage > self.totalPage) {
        self.loading = true;
        return false;
      }
      self.getData();
    },
    format(date) {
      if (date) {
        let d = new Date(date).Format("yy-MM-dd hh:mm:ss"); //"2018-11-15 17:40:00"
        return d;
      }
    },
    changeAllState() {
      this.checkedAll === true
        ? (this.checkedAll = false)
        : (this.checkedAll = true);
      this.checkModel = [];
      if (this.checkedAll === true) {
        this.listData.forEach((value, index) => {
          this.checkModel.push(value.taskId);
        });
      }
    },
    submit() {
      // var v = document.getElementById("addNew").value;
    },
    getData() {
      let self = this;
      if (self.market.supermarketId === null) {
        self.errMsg = "请先选择超市";
        setTimeout(() => {
          self.errMsg = "";
        }, 1000);
      }
      let url =
        self.tabTitleIndex === 0
          ? "/correct/correct/getTaskListBySupermarketId.do"
          : "correct/correct/getNotCompleteTask.do";
      Axios.post(url, {
        currentPage: self.currentPage,
        pageSize: self.pageSize,
        supermarketId: self.market.supermarketId,
        categoryId: self.categoryId,
        screenType: self.screenType
      }).then(res => {
        let data = res.data;
        if (res.respCode === "S") {
          if (self.currentPage == 1 && data.length > 0) {
            self.checkModel = [];
            self.listData = data.data;
          } else {
            self.listData = self.listData.concat(data.data);
          }
          self.totalPage = data.totalPage;

          if (self.currentPage < self.totalPage) {
            self.loading = false;
          }
        } else {
          self.listData = [];
        }
        if (self.tabTitleIndex === 1 && self.listData.length > 0) {
          self.upDataDate();
        }
      });
    },

    postTaskId() {
      let self = this;
      Axios.post("/correct/correct/grabSheet.do", {
        taskIdList: self.checkModel
      }).then(function (data) {
        if (data.respCode === "S") {
          self.pop.grabSheetNot = data.data.grabSheetNot;
          self.pop.grabSheetPass = data.data.grabSheetPass;
          self.pop.show = true;
          self.listData = [];
          self.checkModel = [];
          self.getData();
        } else {
          self.checkModel = [];
          self.errMsg = data.errorMsg;
          setTimeout(() => {
            self.errMsg = "";
          }, 1000);
        }
      });
    },
    //修改价格任务详情 TODO
    toDetails(item, index) {
      let self = this;
      if (self.tabTitleIndex === 0) {
        return;
      }
      self.headerTitle = "任务详情";
      self.skuInfo = item;
      self.skuInfo.index = index;
      self.params.type = this.transformType(item.startType);
      item.startTime != undefined
        ? (self.params.startTime = this.dateFormat(item.startTime))
        : null;
      item.endTime != undefined
        ? (self.params.endTime = this.dateFormat(item.endTime))
        : null;
      self.showType = 2;
      item.price ? (self.showPrice = item.price / 100) : null;
    },

    /**
     * 无此商品
     * taskId 任务id
     * correctId  纠错id 有就必传
     * priceType 没价格：NO_PRICE待审核：WAIT_AUDIT已生效：AFFECTED'
     */
    notGoodsSku() {
      let self = this;
      this.$createDialog({
        type: "confirm",
        title: "暂无价格",
        content: "请确认是否暂无此商品！",
        confirmBtn: {
          text: "确定",
          active: true,
          disabled: false,
          href: "javascript:;"
        },
        cancelBtn: {
          text: "取消",
          active: false,
          disabled: false,
          href: "javascript:;"
        },
        onConfirm: () => {
          Axios.post("/correct/correct/notGoodsSku.do", {
            goodsSkuId: self.listData[self.skuInfo.index].goodsSkuId,
            supermarketId: self.market.supermarketId,
            taskId: self.listData[self.skuInfo.index].taskId,
            correctId: self.listData[self.skuInfo.index].correctId,
            priceType: self.listData[self.skuInfo.index].priceType
          }).then(function (data) {
            if (data.respCode === "S") {
              self.toast = self.$createToast({
                txt: data.data,
                type: "txt"
              });
              self.toast.show();
              self.currentPage = 1;
              self.createTimeStart = "";
              self.createTimeEnd = "";
              self.resetShareAndCorrection();
              self.listData = [];
              self.getData();
              self.showType = 0;
              self.headerTitle = "价格纠错/分享";
            } else {
              this.toast = this.$createToast({
                txt: "反馈失败",
                type: "txt"
              });
              this.toast.show();
            }
          });
        },
        onCancel: () => { }
      }).show();
    },
    // 右侧筛选弹窗选择分类
    clickCategory(item) {
      const screenPopup = this.$refs.screenPopup;
      screenPopup.hide();
      this.currentPage = 1;
      this.pullUpLoad = false;
      this.categoryId = item.categoryId;
      this.getData();
    },
    //倒计时
    upDataDate() {
      let T = "";
      let clock = window.setInterval(() => {
        for (let i = 0; i < this.listData.length; i++) {
          var R = new Date(this.listData[i].expireTime) - new Date(); //剩余毫秒数
          let H = "";
          let M = "";
          let S = "";
          if (R <= 0) {
            this.listData.splice(i, 1);
            //返回待完成
            if (this.showType === 2) {
              this.showType = 0;
              this.tabTitleIndex = 1;
            }
          } else {
            D = parseInt(R / 1000 / 60 / 60 / 24, 10);
            D <= 0 ? (D = "") : (D = D + ":");

            H = parseInt((R / 1000 / 60 / 60) % 24, 10);
            H < 10 ? (H = "0" + H) : H;

            M = parseInt((R / 1000 / 60) % 60, 10); //剩余分钟
            M < 10 ? (M = ":0" + M) : (M = ":" + M);

            S = parseInt((R / 1000) % 60, 10); //剩余秒数
            S < 10 ? (S = ":0" + S) : (S = ":" + S);

            T = "倒计时：" + D + H + M + S;
            this.listData[i].createTime = T;
          }
        }
      }, 1000);
    },
    filter(str) {
      this.screenType = str;
      this.listData = [];
      this.currentPage = 1;
      this.getData();
    },
    dateFormat(date) {
      let d = new Date(date).Format("yy-MM-dd hh:mm");
      return d;
    },
    transformType(type) {
      if (type === undefined) {
        return;
      } else if (type === "NORMAL") {
        return "正常价";
      } else if (type === "PROMOTION") {
        return "促销价";
      } else if (type === "DEAL") {
        return "处理价";
      } else if (type === "MEMBER") {
        return "会员价";
      }
    },
    historyScreen() {
      this.historyScreenTime();
      this.screenTimePicker.show();
    },
    // 价格纠错开始时间和结束时间弹窗初始化
    historyScreenTime() {
      let dateSegmentData = [
        {
          is: "cube-date-picker",
          title: "开始时间",
          min: new Date(2019, 0, 1),
          max: new Date(2030, 12, 31),
          columnCount: 3
        },
        {
          is: "cube-date-picker",
          title: "结束时间",
          min: new Date(2019, 0, 1),
          max: new Date(2030, 12, 31),
          columnCount: 3
        }
      ];
      this.screenTimePicker = this.$createSegmentPicker({
        data: dateSegmentData,
        onSelect: (selectedDates, selectedVals, selectedTexts) => {
          let startTime = selectedTexts[0];
          let endTime = selectedTexts[1];
          this.startTime =
            startTime[0] +
            "-" +
            startTime[1] +
            "-" +
            startTime[2] +
            " 00:00:00";
          this.endTime =
            endTime[0] + "-" + endTime[1] + "-" + endTime[2] + " 23:59:59";
          this.currentPage = 1;
          this.pullUpLoad = false;
          this.getHistoryData();
        },
        onNext: (i, selectedDate, selectedValue, selectedText) => {
          dateSegmentData[1].min = selectedDate;
          if (i === 0) {
            this.screenTimePicker.$updateProps({
              data: dateSegmentData
            });
          }
        }
      });
    }
  }
});
